﻿using System;
using System.Web.Mvc;

using mvcForum.Core;
using mvcForum.Core.Abstractions.Interfaces;
using mvcForum.Core.Specifications;
using mvcForum.Web.Results;
using mvcForum.Core.Interfaces.Data;

namespace mvcForum.Web.Attributes {

	public class NotBannedAttribute : FilterAttribute, IAuthorizationFilter {
		private object typeId;

		public NotBannedAttribute() :
			base() {

			this.typeId = new Object();
		}

		public override object TypeId {
			get {
				return base.TypeId;
			}
		}

		public void OnAuthorization(AuthorizationContext filterContext) {
			if (filterContext == null) {
				throw new ArgumentNullException("filterContext");
			}
			IRepository<BannedIP> repository = DependencyResolver.Current.GetService<IRepository<BannedIP>>();
			if (repository.ReadOne(new BannedIPSpecifications.SpecificIP(filterContext.HttpContext.Request.UserHostAddress)) != null) {
				// TODO: Get the text from the TextManager instead!
				filterContext.Result = new BannedResult("You're IP address has been banned. Please contact us for further details.");
			}
		}
	}
}